//
//  CGQduankouDetailViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/5/11.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import MJRefresh
import RxSwift
import RxDataSources
import Then
import SDCycleScrollView
import IQKeyboardManagerSwift
class CGQduankouDetailViewController: SYBaseTableViewController {
    
    
    var  model:NSMessageRecordsModel!

    var type: String!
    
    var  dicParams: [String : String] = [:]
    
    
    
    init(model: NSMessageRecordsModel, type: String) {
        super.init(nibName: nil, bundle: nil)
        self.model = model
        self.type = type
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setTableView()
        setNoDataView()
        loadDataSuccessed()
        self.title = "端口详情"
        self.view.backgroundColor = Constant.bgViewColor
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.enableAutoToolbar = true
    }
    
    
    
    @objc func rightAction() {
        
    }
    
    
    
    func loadDataSuccessed() {
        DispatchQueue.main.async {
            
            self.setDataSource()
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT-64.autoWidth()), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.backgroundColor = .clear
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.view.addSubview(self.tableView!)
        
        tableView.rx.itemSelected
            .subscribe(onNext:{ [weak self] indexPath in
                if let `self` = self {
                    
                }
               
            })
            .disposed(by:dispose)
        
     
    }
    
    @objc func saveEditInfo(btn:UIButton){
        
        if self.dicParams.count == 0 {
            
            return
            
        }
        
        print(self.dicParams ?? "")
        
        requestVM.baseRequest(disposeBag: dispose, type: .editDeviceDetail(dic: self.dicParams), modelClass: SGBaseModel<CGQshebeiinfoModel>.self) {[weak self] (res) in
            
            if let `self` = self  {
        
                self.popVC()
                
//                self.loadDataSuccessed()
            }
            
        } Error: {
            
        }
        
        
    }
    
    func setNoDataView() {
        
        
        
     
        
        noDataView = SYNoDataView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT))
        noDataView.isHidden = true
        tableView.addSubview(noDataView)
    }
    
    func setDataSource() {
        //初始化数据
        let sections = Observable.just([
            CGQshebeiinfoModelSection(header: "", items: [CGQshebeiinfoModel()])//self.vm.dataSource)
        ])
        
        //创建数据源
        let dataSource = RxTableViewSectionedAnimatedDataSource<CGQshebeiinfoModelSection>(
           //设置单元格
            configureCell: { ds, tv, ip, item in
                
                let cell: CGQduankouDetailTableViewCell = tv.dequeueReusableCell(withIdentifier: "Cell")
                    as? CGQduankouDetailTableViewCell ?? CGQduankouDetailTableViewCell(style: .default, reuseIdentifier: "Cell")
                cell.selectionStyle = .none
                cell.configureMessageView(model: self.model, type: self.type)
                return cell
                
            },
            //设置分区头标题
            titleForHeaderInSection: { ds, index in
                return ds.sectionModels[index].header
            })
        tableView.dataSource = nil
        //绑定单元格数据
        sections
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: dispose)
        
    }
    
    
}
